
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yysk_app/model/collection_model/my_collection_list_by_collection_detail_model.dart';
import 'package:yysk_app/model/common_model/notice_list_model.dart';
import 'package:yysk_app/net/api/common_api.dart';
import 'package:yysk_app/net/api/product_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_choose_platform.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_confirm_widget.dart';


class MyCollectionDetailController extends GetxController {
  final isRead = true.obs;
  final buyNum = 1.obs;
  final maxNum = 10;
  final price = 100.00;
  final txt = '转赠'.obs;
  int pageNo = 1;
  final pageSize = 10;
  final firstDetail = Rx<dynamic?>(null);
  final currentTab = 'detail'.obs;
  late String collectionId;
  late String? marketType;
  late String? id;
  //出售控制器
    final TextEditingController saleController = TextEditingController();
  final payPassword = ''.obs; //输入支付密码
  final selectedList = RxList<int>();
  //数据详情
  final detail = Rx<MyCollectionListByCollectionDetailModel?>(null);

  final ScrollController scrollController = ScrollController();
    //相关公告
  int pageNum = 1;
  final noticeList = RxList<NoticeListItem>();
  var headPrice='';
  var serviceCharge = '';
  @override
  void onInit() {
    super.onInit();
    id = Get.parameters['id'] ?? Get.arguments['id'] ?? 0;
        headPrice = Get.arguments['headPrice'] ?? '';
     serviceCharge = Get.arguments['serviceCharge'] ?? '';
      getdetail().then((res){
        getNoticeList();
    });
  }

  @override
  void dispose() {
    super.dispose();
    Get.delete<ScrollController>(); //删除控制器实例
    scrollController.dispose();
    saleController.dispose();
  }

  void add() {
    if (buyNum.value < maxNum) {
      buyNum.value++;
    }
  }

  void subtract() {
    if (buyNum.value > 1) {
      buyNum.value--;
    }
  }

  //头部数据
  Future<void> getdetail()async {
    http.netFetch<MyCollectionListByCollectionDetailModel>(
      '${ProductApi.userCollectionDetail}/$id',
      params: {'id': id},
      method: Method.get,
      success: (result) {
        detail.value = result.data;
      },
    );
  }
//todu紧急写法，后续优化
  void confirmAction() {
    List list = [];
    list.add(detail.value?.id);
    if(detail.value?.transferable==true&&detail.value?.externalTransferable==false){
       Get.toNamed(
        DanationConfirmWidget.path,
        arguments: {
          "list": list,
          'getTokentype': '1',
          'url': detail.value?.mainImageUrl,
          'collectionId': int.parse(detail.value!.collectionId!),
          "type": '1',
          "transferTypeWay": 1,
        },
      );
       return;
    }else if(detail.value?.transferable==false&&detail.value?.externalTransferable==true){
       Get.toNamed(
        DanationConfirmWidget.path,
        arguments: {
          "list": list,
          'getTokentype': '1',
          'url': detail.value?.mainImageUrl,
          'collectionId': int.parse(detail.value!.collectionId!),
          "type": '2',
          "transferTypeWay": 1,
        },
      );
      return;
    }
    showDanationChoosePlatformDialog(context: Get.context!).then((value) {
      if (value == '0') {
        return;
      }
      Get.toNamed(
        DanationConfirmWidget.path,
        arguments: {
          "list": list,
          'getTokentype': '1',
          'url': detail.value?.mainImageUrl,
          'collectionId': int.parse(detail.value!.collectionId!),
          "type": value,
          "transferTypeWay": 1,
        },
      );
    });
  }
        //获取notice公告数据
  Future<void> getNoticeList({loadMore = false,id=0}) {

    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      return http.netFetch<NoticeListModel>(
        CommonApi.noticeList,
        method: Method.post,
        params: {'pageNum': pageNum, 'pageSize': 100,"collectionId": detail.value?.collectionId},
        success: (result) {
          List<NoticeListItem> items = result.data?.list ?? [];
   
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            noticeList.clear();
          }
          noticeList.addAll(items);
        },
      );
    } catch (e) {
      debugPrint(e.toString());
      if (loadMore) {
        pageNum--;
      }
      return Future.error(e);
    }
  }
  
  unSaleList({required List<String> dataList, required String password}) {
    http.netFetch<dynamic>(
      ProductApi.cancelSaleOrder,
      params: {'saleOrderIds': dataList, 'payPassword': password},
      method: Method.post,
      success: (result) {
        if (result.code == 200) {
          showToast('已取消出售');
          getdetail();
        }
      },
    );
  }
}
